package cn.slipi.core.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * AbstractFilter
 */
public abstract class AbstractFilter implements WebFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        return doFilter(exchange, chain)
                .switchIfEmpty(Mono.just(false))
                .flatMap(result -> result ? chain.filter(exchange) : doDenyResponse(exchange));
    }

    protected abstract Mono<Boolean> doFilter(ServerWebExchange exchange, WebFilterChain chain);

    protected abstract Mono<Void> doDenyResponse(ServerWebExchange exchange);
}
